var $,
    layer,
    href = location.href,
    length = href.length,
    index = href.indexOf('='),
    id = decodeURI(href.substr(index + 1, length - index));

var user = JSON.parse(localStorage.getItem('user'));
layui.use(['jquery', 'layer'], function () {
   $ = layui.jquery,
       layer = layui.layer;
});

//给input赋值
$.ajax({
    url: '/Product/getByIdProduct',
    data: {'productId' : id},
    dataType: 'json',
    success: function (item) {
        $('#ArticleTitle').text(item.data.productName);
        $('#UploadTime').text(item.data.createDate);
        $('#Uploader').text(item.data.userName);
        $('#ArticlePrice').text(item.data.currentpriceProduct);
        $('#ArticleSearchKeywords').text(item.data.searchLeywords);
        $('#ArticlePraise').text(item.data.orderCount);
        $('#Hits').text(item.data.hits);
        if (item.data.productSate == 0){
            item.data.productSate = '已删除';
        } else if(item.data.productSate == 1){
            item.data.productSate = '上架';
        }else if(item.data.productSate == 2){
            item.data.productSate = '下架';
        }
        $('#ArticleStatus').text(item.data.productSate);
        $('#ArticleClassification').text(item.data.title);
        $('#Preview').attr('src', item.data.mainImage);
        $('#previewpre').text(item.data.productDescription);
        $('#userName').val(user.trueName);
        $('#account').val(user.account);
    }
});

//添加评论记录
function submit(){
    var content = $('textarea[name=content]').val();
    var userType;
    var date = getDate();
    if (user.trueName == undefined){
        userType = 1;
    }else{
        userType = 2;
    }
    $.ajax({
        url: '/comment/addComment',
        data: {
            'userId': user.userId,
            'commodityId': id,
            'type': 3,
            'userType': userType,
            'status': 1,
            'content': content,
            'parentId': 0,
            'commentDate': date,
            'display': 2,
            'fabulous': 0,
            'reply': 0,
            'createId': user.userId,
            'createDate': date,
            'updateId': user.userId,
            'updateDate': date,
        },
        dataType: 'json',
        success: function (item) {
            if (item.data){
                parent.location.reload();
            } else{
                layer.msg('评论失败');
            }
        }
    });
};

//获取时间
function getDate() {
    var date = new Date().toLocaleString('zh', { hour12: false }).split('/').join('-');
    var temp = date.substring(0, 9);
    date = date.substr(date.lastIndexOf('-') + 3);
    date = temp + date;
    return date;
}

//查询所有评论
$.ajax({
    url: '/comment/getAllCommentContent',
    data: {
        'commodityId' : id,
        'type' : 3
    },
    dataType: 'json',
    success: function (item) {
        item.data.forEach(function (ele) {
            if (ele.userType == 2){
                if (ele.status == 1) {
                    $('#commentContent').append(
                        '<div style="margin: 20px">' +
                            '<div style="line-height: 30px">&nbsp;&nbsp;' +
                                '<img id="img" src="' + ele.afterUserUrl + '" style="width: 30px;height: 30px; background: pink;float: left;border-radius: 50%"/>&nbsp;&nbsp;' +
                                '<span id="cousterName">' + ele.afterUserAccount + '</span>&nbsp;&nbsp;' +
                            '</div>' +
                            '<div>&nbsp;&nbsp;' +
                                '<span id="content">' + ele.content + '</span>&nbsp;&nbsp;' +
                            '</div>&nbsp;&nbsp;' +
                            '<div style="margin: -10px 0 5px">' +
                                '<span id="createTime">' + ele.commentDate + '</span>&nbsp;&nbsp;' +
                                '<span id="status">正常</span>&nbsp;&nbsp;' +
                            '</div>' +
                        '</div>');
                } else if (ele.status == 2) {
                    $('#commentContent').append(
                        '<div style="margin: 20px">' +
                        '<div style="line-height: 30px">&nbsp;&nbsp;' +
                        '<img id="img" src="' + ele.afterUserUrl + '" style="width: 30px;height: 30px; background: pink;float: left;border-radius: 50%"/>&nbsp;&nbsp;' +
                        '<span id="cousterName">' + ele.afterUserAccount + '</span>&nbsp;&nbsp;' +
                        '</div>' +
                        '<div>&nbsp;&nbsp;' +
                        '<span id="content">' + ele.content + '</span>&nbsp;&nbsp;' +
                        '</div>&nbsp;&nbsp;' +
                        '<div style="margin: -10px 0 5px">' +
                        '<span id="createTime">' + ele.commentDate + '</span>&nbsp;&nbsp;' +
                        '<span id="status">已拦截</span>&nbsp;&nbsp;' +
                        '</div>' +
                        '</div>');
                } else if (ele.status == 3) {
                    $('#commentContent').append(
                        '<div style="margin: 20px">' +
                        '<div style="line-height: 30px">&nbsp;&nbsp;' +
                        '<img id="img" src="' + ele.afterUserUrl + '" style="width: 30px;height: 30px; background: pink;float: left;border-radius: 50%"/>&nbsp;&nbsp;' +
                        '<span id="cousterName">' + ele.afterUserAccount + '</span>&nbsp;&nbsp;' +
                        '</div>' +
                        '<div>&nbsp;&nbsp;' +
                        '<span id="content">' + ele.content + '</span>&nbsp;&nbsp;' +
                        '</div>&nbsp;&nbsp;' +
                        '<div style="margin: -10px 0 5px">' +
                        '<span id="createTime">' + ele.commentDate + '</span>&nbsp;&nbsp;' +
                        '<span id="status">已删除</span>&nbsp;&nbsp;' +
                        '</div>' +
                        '</div>');
                }
            } else{
                if (ele.status == 1) {
                    $('#commentContent').append(
                        '<div style="margin: 20px">' +
                        '<div style="line-height: 30px">&nbsp;&nbsp;' +
                        '<img id="img" src="' + ele.beforeUserUrl + '" style="width: 30px;height: 30px; background: pink;float: left;border-radius: 50%"/>&nbsp;&nbsp;' +
                        '<span id="cousterName">' + ele.beforeUserAccount + '</span>&nbsp;&nbsp;' +
                        '</div>' +
                        '<div>&nbsp;&nbsp;' +
                        '<span id="content">' + ele.content + '</span>&nbsp;&nbsp;' +
                        '</div>&nbsp;&nbsp;' +
                        '<div style="margin: -10px 0 5px">' +
                        '<span id="createTime">' + ele.commentDate + '</span>&nbsp;&nbsp;' +
                        '<span id="status">正常</span>&nbsp;&nbsp;' +
                        '</div>' +
                        '</div>');
                } else if (ele.status == 2) {
                    $('#commentContent').append(
                        '<div style="margin: 20px">' +
                        '<div style="line-height: 30px">&nbsp;&nbsp;' +
                        '<img id="img" src="' + ele.beforeUserUrl + '" style="width: 30px;height: 30px; background: pink;float: left;border-radius: 50%"/>&nbsp;&nbsp;' +
                        '<span id="cousterName">' + ele.beforeUserAccount + '</span>&nbsp;&nbsp;' +
                        '</div>' +
                        '<div>&nbsp;&nbsp;' +
                        '<span id="content">' + ele.content + '</span>&nbsp;&nbsp;' +
                        '</div>&nbsp;&nbsp;' +
                        '<div style="margin: -10px 0 5px">' +
                        '<span id="createTime">' + ele.commentDate + '</span>&nbsp;&nbsp;' +
                        '<span id="status">已拦截</span>&nbsp;&nbsp;' +
                        '</div>' +
                        '</div>');
                } else if (ele.status == 3) {
                    $('#commentContent').append(
                        '<div style="margin: 20px">' +
                        '<div style="line-height: 30px">&nbsp;&nbsp;' +
                        '<img id="img" src="' + ele.beforeUserUrl + '" style="width: 30px;height: 30px; background: pink;float: left;border-radius: 50%"/>&nbsp;&nbsp;' +
                        '<span id="cousterName">' + ele.beforeUserAccount + '</span>&nbsp;&nbsp;' +
                        '</div>' +
                        '<div>&nbsp;&nbsp;' +
                        '<span id="content">' + ele.content + '</span>&nbsp;&nbsp;' +
                        '</div>&nbsp;&nbsp;' +
                        '<div style="margin: -10px 0 5px">' +
                        '<span id="createTime">' + ele.commentDate + '</span>&nbsp;&nbsp;' +
                        '<span id="status">已删除</span>&nbsp;&nbsp;' +
                        '</div>' +
                        '</div>');
                }
            }
        });
    }
});